package com.ttc.mBackup.common;

import java.io.File;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.dropbox.client.DropboxAPI;
import com.dropbox.client.DropboxAPI.Config;
import com.ttc.mBackup.BackupAgent.BARAddTask;
import com.ttc.mBackup.BackupAgent.LoginDropbox;

public class uploaddropbox extends AsyncTask<Void, Void, Integer> {

	private static final String TAG = "UploadDropBox";
	private File file;
	DropboxAPI.Config mConfig;
	DropboxAPI.Account mAccount;
    private DropboxAPI api = new DropboxAPI();
    //private LoginDropbox login = new LoginDropbox();
    private String email;
    private String pass;
    private String dir;
    private Context context;
   
    public uploaddropbox(Context cont,File file, String email, String pass, String dir)
	{
		super();
		this.file=file;
		this.context=cont;
		this.email=email;
		this.pass=pass;
		this.dir=dir;
	}
	
	@Override
	protected Integer doInBackground(Void... params) {
		// TODO Auto-generated method stub
		int success = DropboxAPI.STATUS_NONE;
		
		if (mConfig == null) {
	    	mConfig = api.getConfig(null, false);
	    	// TODO On a production app which you distribute, your consumer
	    	// key and secret should be obfuscated somehow.
	    	mConfig.consumerKey=LoginDropbox.CONSUMER_KEY;
	    	mConfig.consumerSecret=LoginDropbox.CONSUMER_SECRET;
	    	mConfig.server="api.dropbox.com";
	    	mConfig.contentServer="api-content.dropbox.com";
	    	mConfig.port=80;
    	}
		
		try{
    	if (!api.isAuthenticated()) {
    		
            mConfig = api.authenticate(mConfig, email, pass);
            
            this.setConfig(mConfig);

            success = mConfig.authStatus;

            if (success != DropboxAPI.STATUS_SUCCESS) {
            	return success;
            }
    	}
    	mAccount = api.accountInfo();

    	if (!mAccount.isError()) {
    		return DropboxAPI.STATUS_SUCCESS;
    	} else {
    		Log.e(TAG, "Account info error: " + mAccount.httpCode + " " + mAccount.httpReason);
    		return DropboxAPI.STATUS_FAILURE;
    	}
    } catch (Exception e) {
        Log.e(TAG, "Error in logging in.", e);
        return DropboxAPI.STATUS_NETWORK_ERROR;
    }
    
}

	@Override
	protected void onPostExecute(Integer result) {
		// TODO Auto-generated method stub
		if (result == DropboxAPI.STATUS_SUCCESS) {
        	if (mConfig != null && mConfig.authStatus == DropboxAPI.STATUS_SUCCESS) {
            	
            	//this.setLoggedIn(true);
            	
            //	login.showToast("Logged into Dropbox");
            	if(file.isFile())
            		{
            		if(context!=null){
            		
            			api.putFile("dropbox",dir , file);
            			
            			
            			
            		}
            		}
            	else
            	{
            		File[] all=file.listFiles();
            		for(int i =0;i<all.length;i++)
            		{
            			
            		api.putFile("dropbox",dir , all[i]);
            		
            		}
            	}
            	Toast.makeText(context, "Upload Success ....",Toast.LENGTH_SHORT).show();	
 	
        } else {
        	if (result == DropboxAPI.STATUS_NETWORK_ERROR) {
        		Toast.makeText(context, "Network error: " + mConfig.authDetail,Toast.LENGTH_SHORT).show();
        		
        	
	            } else {
	            	Toast.makeText(context,"Unsuccessful login." ,Toast.LENGTH_SHORT).show();
	           
	            }
        	}
  
		}
		BARAddTask.upResult(true);
		
	}
	
	 
	
	private void setConfig(Config conf) {
	    	mConfig = conf;
	    }
	 
	 
	   /**
	     * Convenience function to change UI state based on being logged in
	     */
	// private void setLoggedIn(boolean loggedIn) {
		 	
	  //  }
	
		 
		
	 
}
